package com.dckeji.shiplock.webapp.controller.rest;

import com.alibaba.fastjson.JSONObject;
import com.dckeji.shiplock.framework.constants.SystemConstants;
import com.dckeji.shiplock.framework.util.HttpClientUtil;
import com.dckeji.shiplock.framework.util.SingleAccessToken;
import com.dckeji.shiplock.framework.util.WeChatConf;
import com.dckeji.shiplock.framework.util.WeChatUtil;
import com.dckeji.shiplock.webapp.controller.BaseController;
import com.dckeji.shiplock.webapp.pojo.LogicRsult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/8/12
 * Time: 14:03
 */
@RestController
@RequestMapping("/rest/tool/")
public class ToolController extends BaseController {

    @Value("${wx.appid}")
    String appid;
    @Value("${wx.appSecret}")
    String appSecret;
    @Value("${wx.accessToken}")
    String url;
    @Value("${wx.ticket}")
    String ticketUrl;
    @Value("${wx.webAccessToken}")
    String webAccessToken;
    @Value("${wx.userInfo}")
    String userInfoUrl;


    @RequestMapping("getJsSdkRequire")
    public LogicRsult getJsSdkRequire() {
        LogicRsult logicRsult = new LogicRsult();
        try {
//            String referer = request.getHeader("Referer");
            String referer = "http://localhost:8080/text.jsp";
            String replace = this.url.replace("APPID", appid).replace("APPSECRET", appSecret);
            SingleAccessToken accessToken = SingleAccessToken.getInstance(replace, ticketUrl);
            WeChatConf weChatConf = accessToken.getAccessToken();
            String timestamp = Long.toString(System.currentTimeMillis() / 1000);
            String noncestr = UUID.randomUUID().toString().replace("-", "");
            String signature = WeChatUtil.sin(weChatConf.getTicket(), timestamp, noncestr, referer);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("appid", appid);
            jsonObject.put("timestamp", timestamp);
            jsonObject.put("noncestr", noncestr);
            jsonObject.put("signature", signature);
            logicRsult.setObj(jsonObject);
            logicRsult.setResultCode(SystemConstants.ResultCode.SUCCESS);
            logicRsult.setResultMsg(SystemConstants.ResultMsg.SUCCESS);
        } catch (Exception e) {
            logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
            logicRsult.setResultMsg(e.getMessage());
            e.printStackTrace();
        }


        return logicRsult;
    }


    @RequestMapping("auth")
    public LogicRsult wxAuth(String code) throws InterruptedException {
        LogicRsult logicRsult = new LogicRsult();
        //获取网页授权AccessToken
        String accessTokenUrl = String.format(webAccessToken, appid, appSecret, code);
        HttpClientUtil httpClientUtil = HttpClientUtil.getInstance();
        String wxUserInfo = httpClientUtil.sendData4Get(accessTokenUrl);
        JSONObject jsonObject = JSONObject.parseObject(wxUserInfo);
        String access_token = jsonObject.getString("access_token");
        String openid = jsonObject.getString("openid");
        String format = String.format(userInfoUrl, access_token, openid);
        String userInfo = httpClientUtil.sendData4GetHTTPS(format);
        JSONObject ui = JSONObject.parseObject(userInfo);
        logicRsult.setObj(ui);
        System.out.println(JSONObject.toJSONString(logicRsult));
        return logicRsult;
    }


}
